<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\FieldModel;
use app\admin\model\CountryModel;
use app\admin\model\MakeModel;

class Country extends Base
{
	/**
	 * 领域对象
	 * @return mixed
	 */
	public $field_obj;
	
	/**
	 * 国家对象
	 * @return mixed
	 */
	public $country_obj;
	
	/**
	 * 制造商对象
	 * @return mixed
	 */
	public $make_obj;
	
	/**
	 * 构造函数
	 * @return mixed
	 */
	public function __construct()
	{
		parent::__construct();
		$this->field_obj = new FieldModel(); 
		$this->country_obj = new CountryModel();
		$this->make_obj = new MakeModel();
	}
	
	/**
	 * 国家区域
	 * @return mixed
	 */
    public function lists()
    {
		$input = input('get.');
		if(isset($input['data'])){
			$data = $this->country_obj->getFieldCountryList($input);
			if(!empty($data['list']['data'])){
				foreach($data['list']['data'] as $key=>$val){
					$fieldObj = FieldModel::get($val['field_id']);
					$countryObj = CountryModel::get($val['country_id']);
					$data['list']['data'][$key]['field_country_code'] = $fieldObj->field_code." - ".$countryObj->country_code;
					$data['list']['data'][$key]['field_country_name'] = $fieldObj->field_name." - ".$countryObj->country_name;
					if(is_numeric($val['create_time'])){
						$data['list']['data'][$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
					}
				}
			}
			$result = array('code'=>0,'msg'=>'','count'=>$data['list']['total'],'data'=>$data['list']['data']);
			return json($result);
		}else{
			return view('index');
		}
    }
	
	/**
	 * 新增国家页面
	 * @return mixed
	 */
	public function add()
	{
		$fieldData = array();
		$field = $this->field_obj->where('field_status','in',[0,1])->select();
		foreach($field as $_f){
			$fieldData[$_f['id']] = $_f['field_name'];
		}
		$this->assign('field', $fieldData);
		return view('add');
	}
	/**
	 * 编辑国家
	 * @return mixed
	 */
	public function edit()
	{
		$input = input('get.');
		$view = CountryModel::get($input['id'])->toArray();
		$field = $this->field_obj->where('field_status','in',[0,1])->select();
		foreach($field as $_f){
			$fieldData[$_f['id']] = $_f['field_name'];
		}
		$where = array();
		$where['country_id'] = $input['id'];
		$field = $this->country_obj->getCountryRela($where);
		$fieldArr = array();
		if(!empty($field)){
			foreach($field as $_f){
				array_push($fieldArr,$_f['field_id']);
			}
		}
		$this->assign('field', $fieldData);
		$this->assign('field_arr', $fieldArr);
		$this->assign('view', $view);
		return view('edit');
	}
	
	/**
	 * 更新国家
	 * @return mixed
	 */
	public function save()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$field_id = isset($input['field_id']) ? $input['field_id'] : 0;
			unset($input['field_id']);
			if(isset($input['id']) && intval($input['id']) > 0){
				$input['update_time'] = time();
				$flag = $this->country_obj->save($input,['id'=>$input['id']]);
			}else{
				$input['create_time'] = time();
				$input['update_time'] = time();
				$this->country_obj->data($input);
				$flag = $this->country_obj->save();
				$input['id'] = $this->country_obj->id;
			}
			if($field_id > 0){
				$input['field_id'] = $field_id;
				$flag = $this->country_obj->insertCountryRela($input);
			}
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新国家数据，请求不合法！');
		}
	}	
	
	/**
	 * 删除国家
	 * @return mixed
	 */
	public function del()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$where = array();
			$where['country_id'] = $input['id'];
			$country = $this->make_obj->getMakeList($where);
			if(!empty($country['list'])){
				$result = [
					'code' => 0,
					'msg'  => '国家已经关联制造商，不允许删除！',
					'list' => $input
				];
			}else{
				$field = CountryModel::get($input['id']);
				$field->country_status = -1;
				$flag = $field->save();
				if($flag){
					$result = [
						'code' => 1,
						'msg'  => '请求成功',
						'list' => $input
					];
				}else{
					$result = [
						'code' => 0,
						'msg'  => '请求失败',
						'list' => $input
					];
				}
			}
			return json($result);
		}else{
			$this->error('更新国家数据，请求不合法！');
		}
	}
	
	/**
	 * 更新国家状态
	 * @return mixed
	 */
	public function setStatus()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$field = CountryModel::get($input['id']);
			$status = 1;
			if($field->country_status){
				$status = 0;
			}
			$field->country_status = $status;
			$flag = $field->save();
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新国家数据，请求不合法！');
		}
	}
}
